package org.puppet.demo.tank.puppet;

import java.util.Map;

import org.puppet.demo.tank.control.TankController;
import org.puppet.game.World;
import org.puppet.game.element.Puppet;
import org.puppet.game.element.PuppetInfo;

import android.content.Resources;

public abstract class Tank extends Puppet {

	private static int ANI_STAND = 0;
	private static int ANI_WALK = 1;
	private static int ANI_SHOOT = 2;
	private static int ANI_EMPTY = 3;

	protected boolean isShooting = false;
	protected boolean isEmpty = false;
	protected boolean isWalking = false;
	protected boolean isStand = false;

	protected Map<Integer, Bullet> bullets;
	
	protected TankController tankController;

	public float bulletX = 0;
	public float bulletY = 0;

	public Tank() {
		super();
		bullets = getBullets();
		tankController = new TankController(this);
		changeAnimation(ANI_STAND);
	}

	protected abstract Map<Integer, Bullet> getBullets();

	@Override
	public void live() {
		super.live();
		World.current.getCurrentView().getDirector().reg(tankController);
		World.current.reg(tankController);
	}
	
	@Override
	public void die() {
		super.die();
		World.current.getCurrentView().getDirector().unreg(tankController);
		World.current.getDirector().unreg(tankController);
	}

	public void stand() {
		if (!isStand && !isShooting) {
			this.vX = 0;
			this.vY = 0;
			this.acceY = 0;
			this.acceX = 0;
			isWalking = false;
			isStand = true;
			isShooting = false;
			changeAnimation(ANI_STAND);
		}
	}

	public void walk(float vX, float vY) {
		if(!isShooting){
			this.vX = vX;
			this.vY = vY;
			isFaceLeft = vX == 0 ? isFaceLeft : (vX < 0);
			isWalking = true;
			isStand = false;
			changeAnimation(ANI_WALK);
		}
	}

	public void shoot(int bulletId, float vX, float vY) {
		isFaceLeft = vX == 0 ? isFaceLeft : (vX < 0);
		isShooting = true;
		isWalking = false;
		isStand = false;
		bullets.get(bulletId).lunch(vX, vY);
		changeAnimation(ANI_EMPTY);
	}

	public boolean isWalking() {
		return isWalking;
	}

	public boolean isShooting() {
		return isShooting;
	}

	public void setShooting(boolean isShooting) {
		this.isShooting = isShooting;
	}

	public boolean isStand() {
		return isStand;
	}

	public void setStand(boolean isStand) {
		this.isStand = isStand;
	}

	public void setWalking(boolean isWalking) {
		this.isWalking = isWalking;
	}

	public boolean isEmpty() {
		return isEmpty;
	}

	public void setEmpty(boolean isEmpty) {
		this.isEmpty = isEmpty;
	}

	public boolean isFaceLeft() {
		return isFaceLeft;
	}

	public void setFaceLeft(boolean isFaceLeft) {
		this.isFaceLeft = isFaceLeft;
	}

}
